package svc

import (
	"paper-pro/app/paperfile/model"
	"paper-pro/app/paperfile/rpc/internal/config"

	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"github.com/zeromicro/go-queue/kq"
	"github.com/zeromicro/go-zero/core/logx"
)

const (
	PaperFileDbName         = "paper"
	PaperFileCollectionName = "paper_files"
)

type ServiceContext struct {
	Config config.Config

	PaperModel     model.PaperModel
	MinioClient    *minio.Client
	KqPusherClient *kq.Pusher
}

func NewServiceContext(c config.Config) *ServiceContext {
	return &ServiceContext{
		Config: c,

		PaperModel:     model.NewPaperModel(c.MongoDB.Source, PaperFileDbName, PaperFileCollectionName, c.Cache),
		MinioClient:    mustNewMinioClient(&c),
		KqPusherClient: kq.NewPusher(c.KqPusherConf.Brokers, c.KqPusherConf.Topic),
	}
}

func mustNewMinioClient(c *config.Config) *minio.Client {
	minioClient, err := minio.New(c.MinioConf.Endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(c.MinioConf.AccessKey, c.MinioConf.SecretKey, ""),
		Secure: c.MinioConf.UseSSL,
	})
	logx.Must(err)
	return minioClient
}
